<template>
  <div id="certified">
    <h1 class="titleTag">车俩认证</h1>

    <el-form label-width="140px" ref="ruleForm" :model="formData" :inline="true" class="upload">
      <div class="img fs">
        <el-form-item label="上传行驶证" prop="code"></el-form-item>
        <el-form-item prop="code">
          <div class="IMGupload">
            <el-upload class="avatar-uploader" name="imgFile" :data="{type: 3}"
              :action="`${uploadURL}/JysfSysUser/upload`" :show-file-list="false" :on-success="handleSuccess1"
              :before-upload="beforeImageUpload">
              <img v-if="imgurl1" :src="imgurl1" class="avatar">
              <i v-else class="avatar-uploader-icon">
                <img src="../../assets/img/business.png" alt="" class="el-icon-upload" />
                <p class="">请上传行驶证正面</p>
              </i>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item prop="code">
          <div class="IMGupload">
            <el-upload class="avatar-uploader" name="imgFile" :data="{type: 3}"
              :action="`${uploadURL}/JysfSysUser/upload`" :show-file-list="false" :on-success="handleSuccess2"
              :before-upload="beforeImageUpload">
              <img v-if="imgurl2" :src="imgurl2" class="avatar">
              <i v-else class="avatar-uploader-icon">
                <img src="../../assets/img/business.png" alt="" class="el-icon-upload" />
                <p class="">请上传行驶证反面</p>
              </i>
            </el-upload>
          </div>
        </el-form-item>
      </div>

      <div class="img fs">
        <el-form-item label="上传道路运输许可证" prop="code"></el-form-item>
        <el-form-item prop="code">
          <div class="IMGupload">
            <el-upload class="avatar-uploader" name="imgFile" :data="{type: 3}"
              :action="`${uploadURL}/JysfSysUser/upload`" :show-file-list="false" :on-success="handleSuccess3"
              :before-upload="beforeImageUpload">
              <img v-if="imgurl3" :src="imgurl3" class="avatar">
              <i v-else class="avatar-uploader-icon">
                <img src="../../assets/img/business.png" alt="" class="el-icon-upload" />
                <p class="">请上传道路运输许可证正面</p>
              </i>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item prop="code">
          <div class="IMGupload">
            <el-upload class="avatar-uploader" name="imgFile" :data="{type: 3}"
              :action="`${uploadURL}/JysfSysUser/upload`" :show-file-list="false" :on-success="handleSuccess4"
              :before-upload="beforeImageUpload">
              <img v-if="imgurl4" :src="imgurl4" class="avatar">
              <i v-else class="avatar-uploader-icon">
                <img src="../../assets/img/business.png" alt="" class="el-icon-upload" />
                <p class="">请上传道路运输许可证反面</p>
              </i>
            </el-upload>
          </div>
        </el-form-item>
      </div>

      <div class="img fs">
        <el-form-item label="上传车俩正面照片" prop="code"></el-form-item>
        <el-form-item prop="code">
          <div class="IMGupload">
            <el-upload class="avatar-uploader" name="imgFile" :data="{type: 3}"
              :action="`${uploadURL}/JysfSysUser/upload`" :show-file-list="false" :on-success="handleSuccess5"
              :before-upload="beforeImageUpload">
              <img v-if="imgurl5" :src="imgurl5" class="avatar">
              <i v-else class="avatar-uploader-icon">
                <img src="../../assets/img/business.png" alt="" class="el-icon-upload" />
                <p class="">请上传车俩正面照片</p>
              </i>
            </el-upload>
          </div>
        </el-form-item>
      </div>
    </el-form>

    <div v-if="status==0||status==4" class="submitBUTTON mt24">
      <button @click="submitTo()">{{status==4?'修改':'提交'}}</button>
      <button @click="cancle()">取消</button>
    </div>
    <el-dialog title="" :visible.sync="bigImg">
      <img style="width:100%: overflow: hidden" :src="bigImg" alt="" srcset="">
    </el-dialog>
  </div>
</template>

<script>
import { apiCarAdd, apiCarSelect } from "@/utils/api";
import { baseURL } from "@/utils/config";
export default {
  name: "certified",
  data() {
    return {
      bigImg: false,
      status: 0,
      uploadURL: "",
      imgurl1: null,
      imgurl2: null,
      imgurl3: null,
      imgurl4: null,
      imgurl5: null,
      formData: {}
    };
  },
  mounted() {
    this.uploadURL = baseURL;
    this.query();
    console.log(this.$route.query.id);
  },
  methods: {
    // 查询认证信息
    query() {
      //status: (0=未认证，1=已认证，2=待审核，3=未通过)
      apiCarSelect({
        idenid: !this.$route.query.id ? "" : this.$route.query.id
      })
        .then(res => {
          console.log(res);
          this.imgurl1 = res.data.faceImgId;
          this.imgurl2 = res.data.backImgId;
          this.imgurl3 = res.data.faceImg;
          this.imgurl4 = res.data.backImg;
          this.imgurl5 = res.data.headImgId;
          // if (res.data.status == 2) {
          //   this.$message({ message: "审核中", type: "warning" });
          // } else if (res.data.status == 3) {
          //   this.$message({ message: "审核未通过", type: "error" });
          // } else if (res.data.status == 1) {
          //   this.$message({ message: "已认证", type: "success" });
          // }
        })
        .catch(error => {
          console.log(error);
        });
    },
    submitTo() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (!this.imgurl1) {
            this.$message({
              message: "请上传车俩行驶证正面照片",
              type: "error"
            });
          } else if (!this.imgurl2) {
            this.$message({
              message: "请上传车俩行驶证反面照片",
              type: "error"
            });
          } else if (!this.imgurl3) {
            this.$message({
              message: "请上传道路运输证正面照片",
              type: "error"
            });
          } else if (!this.imgurl4) {
            this.$message({
              message: "请上传道路运输证反面照片",
              type: "error"
            });
          } else if (!this.imgurl5) {
            this.$message({ message: "请上传车头正面照片", type: "error" });
          } else {
            let params = {
              idenid: !this.$route.query.id ? "" : this.$route.query.id,
              faceImgId: this.imgurl1,
              backImgId: this.imgurl2,
              faceImg: this.imgurl3,
              backImg: this.imgurl4,
              headImgId: this.imgurl5
            };
            apiCarAdd(params)
              .then(res => {
                this.status = 2;
                this.$message({ message: "提交成功", type: "success" });
              })
              .catch(error => {
                console.log(error);
              });
          }
        } else {
          this.$message({ message: "请完善填写内容", type: "error" });
          return false;
        }
      });
    },
    beforeImageUpload(file) {
      // 上传文件前的钩子对文件进行校验
      console.log(file);
      const size = file.size / 1024 < 1024;
      if (!size) {
        this.$message.error("上传图片大小不能超过 1M!");
      }
      return size;
    },
    handleSuccess1(response, file, fileList) {
      console.log(response);
      this.imgurl1 = response.data;
    },
    handleSuccess2(response, file, fileList) {
      this.imgurl2 = response.data;
    },
    handleSuccess3(response, file, fileList) {
      this.imgurl3 = response.data;
    },
    handleSuccess4(response, file, fileList) {
      this.imgurl4 = response.data;
    },
    handleSuccess5(response, file, fileList) {
      this.imgurl5 = response.data;
    },
    cancle() {
      this.$router.go(0);
    }
  }
};
</script>

<style lang="scss">
.upload {
  .el-upload-dragger {
    width: 346px;
    height: 218px;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 5px;
    margin-top: 10px;
    .el-icon-upload {
      width: 100px;
    }
    .el-upload__text {
      margin-top: -40px;
      color: rgba(0, 0, 0, 0.4);
    }
    .upload-demo {
      display: inline-block;
    }
  }
  .el-upload__text {
    font-size: 14px;
    line-height: 40px;
  }
  .el-form-item__content {
    font-size: 0;
    line-height: 0;
  }
}
</style>
<style lang="scss" scoped>
#certified {
  min-height: 100%;
  background-color: #fff;
  padding: 24px;
  // padding-bottom: 50px;
  box-sizing: border-box;
  .formone {
    margin-top: 24px;
  }
  .edit {
    position: relative;
    button {
      width: 100px;
      height: 40px;
      background: $blue;
      border-radius: 20px;
      color: #fff;
      position: absolute;
      right: 0;
    }
  }
  .upload {
    .upload-demo {
      margin-right: 40px;
      display: inline-block;
    }
    .imgBox {
      width: 346px;
      max-height: 218px;
      border: 1px solid rgba(0, 0, 0, 0.2);
      overflow-y: auto;
      padding: 6px 8px;
      border-radius: 5px;
      box-sizing: border-box;
      img {
        width: 100%;
      }
      &.imgBox1 {
        margin-left: 0px;
      }
    }
    .img {
      align-items: flex-start;
    }
  }
  .IMGupload {
    .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      display: block;
      font-size: 14px;
      color: #8c939d;
      width: 346px;
      height: 218px;
      // line-height: 218px;
      padding-top: 30px;
      text-align: center;
      border: 1px solid rgba(0, 0, 0, 0.2);
      border-radius: 5px;
      box-sizing: border-box;
      .el-icon-upload {
        width: 100px;
      }
    }
    .avatar {
      width: 346px;
      height: 218px;
      display: block;
      padding: 6px;
      border: 1px solid rgba(0, 0, 0, 0.2);
      border-radius: 5px;
      box-sizing: border-box;
    }
  }
}
</style>
